package resumetime;

import org.springframework.util.StopWatch;

import java.util.Arrays;

/**
 * 可方便的对程序部分代码进行计时(ms级别)，适用于同步单线程代码块
 * @author gaofeng
 */
public class StopWatchTest {
    public static void main(String[] args) throws InterruptedException {
        StopWatch stopWatch = new StopWatch() ;

        stopWatch.start("TaskNameOne");
        Thread.sleep(1000 * 3);
        System.out.println("当前线程  : " + stopWatch.currentTaskName());
        stopWatch.stop();

        stopWatch.start("TaskNameTwo");
        Thread.sleep(1000 * 10);
        System.out.println("当前线程  : " + stopWatch.currentTaskName());
        stopWatch.stop();

        stopWatch.start("TaskNameThree");
        Thread.sleep(1000 * 10);
        System.out.println("当前线程  : " + stopWatch.currentTaskName());
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());
        System.out.println(stopWatch.shortSummary());

        // stop 后它的值为null
        System.out.println(stopWatch.currentTaskName());

        System.out.println(stopWatch.getLastTaskName());
        System.out.println(stopWatch.getLastTaskInfo());

        // 任务总的耗时  如果你想获取到每个任务详情（包括它的任务名、耗时等等）可使用
        System.out.println("所有任务总耗时：" + stopWatch.getTotalTimeMillis());
        System.out.println("任务总数：" + stopWatch.getTaskCount());
        System.out.println("所有任务详情：" + Arrays.toString(stopWatch.getTaskInfo()));
    }
}
